package org.openslash;

import java.util.concurrent.BlockingQueue;

import org.openslash.util.AbstractScannerInputMethord;

import mysgs.SGSException;

public class AScannerInputMethord extends AbstractScannerInputMethord{

	private BlockingQueue<String> resp;
	
	public AScannerInputMethord(BlockingQueue<String> response) {
		resp = response;
	}
	
	@Override
	public byte _nextByte() throws SGSException {
		byte b = -1;
		try{
			b = Byte.valueOf(take()).byteValue();
		} catch ( NumberFormatException e )
		{
			e.printStackTrace();
		}
		return b;
	}

	@Override
	public int _nextInt() throws SGSException {
		int value = -1; 
		try{
			value = Integer.valueOf(take()).intValue();
		} catch ( NumberFormatException e )
		{
			e.printStackTrace();
		}
		return value;
	}

	@Override
	public String _nextLine() throws SGSException {
		return take();
	}

	@Override
	public short _nextShort() throws SGSException {
		short s = -1; 
		try{
			s = Short.valueOf(take()).shortValue();
		} catch ( NumberFormatException e )
		{
			e.printStackTrace();
		}
		return s;
	}
	
	public boolean put(String response) {
		try {
			resp.put(response);
		} catch (InterruptedException e) {
			e.printStackTrace();
			return false;
		}
		return true;
	}
	
	private String take() throws SGSException
	{
		String msg = null;
		try {
			msg = resp.take();
		} catch (InterruptedException e) {
			e.printStackTrace();
			throw new SGSException("interrupt");
		}
		if( msg.equals("interrupt") )
		{
			throw new SGSException("interrupt");
		}
		return msg;
	}

	
}
